home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 30 / Mac Magazin and MacEasy Magazine CD - Issue 30.iso / utilities / Mac OS X / Flurry / Flurry source / texture.c < prev   
Text File  |  2001-07-07  |  4KB  |  185 lines

  1. /*
  2.  *  texture.c
  3.  *  hot
  4.  *
  5.  *  Created by calumr on Sat Jul 07 2001.
  6.  *  Copyright (c) 2001 __CompanyName__. All rights reserved.
  7.  *
  8.  */
  9. #import <Carbon/Carbon.h>
  10. #import <AGL/agl.h>
  11. #import <AGL/glu.h>
  12.  
  13. #import "PTypes.h"
  14.  
  15. GLubyte smallTextureArray[32][32];
  16. GLubyte bigTextureArray[256][256][2];
  17. GLuint theTexture = 0;
  18.  
  19. // simple smoothing routine
  20. void SmoothTexture()
  21. {
  22.     GLubyte filter[32][32];
  23.     int i,j;
  24.     float t;
  25.     for (i=1;i<31;i++)
  26.     {
  27.         for (j=1;j<31;j++)
  28.         {
  29.             t = (float) smallTextureArray[i][j]*4;
  30.             t += (float) smallTextureArray[i-1][j];
  31.             t += (float) smallTextureArray[i+1][j];
  32.             t += (float) smallTextureArray[i][j-1];
  33.             t += (float) smallTextureArray[i][j+1];
  34.             t /= 8.0f;
  35.             filter[i][j] = (GLubyte) t;
  36.         }
  37.     }
  38.     for (i=1;i<31;i++)
  39.     {
  40.         for (j=1;j<31;j++)
  41.         {
  42.             smallTextureArray[i][j] = filter[i][j];
  43.         }
  44.     }
  45. }
  46.  
  47. // add some randomness to texture data
  48. void SpeckleTexture()
  49. {
  50.     int i,j;
  51.     int speck;
  52.     float t;
  53.     for (i=2;i<30;i++)
  54.     {
  55.         for (j=2;j<30;j++)
  56.         {
  57.             speck = 1;
  58.             while (speck <= 32 && rand() % 2)
  59.             {
  60.                 t = (float) min(255,smallTextureArray[i][j]+speck);
  61.                 smallTextureArray[i][j] = (GLubyte) t;
  62.                 speck+=speck;
  63.             }
  64.             speck = 1;
  65.             while (speck <= 32 && rand() % 2)
  66.             {
  67.                 t = (float) max(0,smallTextureArray[i][j]-speck);
  68.                 smallTextureArray[i][j] = (GLubyte) t;
  69.                 speck+=speck;
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75. void MakeSmallTexture()
  76. {
  77.     static int firstTime = 1;
  78.     int i,j;
  79.     float r,t;
  80.     if (firstTime)
  81.     {
  82.         firstTime = 0;
  83.         for (i=0;i<32;i++)
  84.         {
  85.             for (j=0;j<32;j++)
  86.             {
  87.                 r = (float) sqrt((i-15.5)*(i-15.5)+(j-15.5)*(j-15.5));
  88.                 if (r > 15.0f)
  89.                 {
  90.                     smallTextureArray[i][j] = 0;
  91.                 }
  92.                 else
  93.                 {
  94.                     t = 255.0f * (float) cos(r*PI/31.0);
  95.                     smallTextureArray[i][j] = (GLubyte) t;
  96.                 }
  97.             }
  98.         }
  99.     }
  100.     else
  101.     {
  102.         for (i=0;i<32;i++)
  103.         {
  104.             for (j=0;j<32;j++)
  105.             {
  106.                 r = (float) sqrt((i-15.5)*(i-15.5)+(j-15.5)*(j-15.5));
  107.                 if (r > 15.0f)
  108.                 {
  109.                     t = 0.0f;
  110.                 }
  111.                 else
  112.                 {
  113.                     t = 255.0f * (float) cos(r*PI/31.0);
  114.                 }
  115.                 smallTextureArray[i][j] = (GLubyte) min(255,(t+smallTextureArray[i][j]+smallTextureArray[i][j])/3);
  116.             }
  117.         }
  118.     }
  119.     SpeckleTexture();
  120.     SmoothTexture();
  121.     SmoothTexture();
  122. }
  123.  
  124. void CopySmallTextureToBigTexture(int k, int l)
  125. {
  126.     int i,j;
  127.     for (i=0;i<32;i++)
  128.     {
  129.         for (j=0;j<32;j++)
  130.         {
  131.             bigTextureArray[i+k][j+l][0] = smallTextureArray[i][j];
  132.             bigTextureArray[i+k][j+l][1] = smallTextureArray[i][j];
  133.         }
  134.     }
  135. }
  136.  
  137. void AverageLastAndFirstTextures()
  138. {
  139.     int i,j;
  140.     int t;
  141.     for (i=0;i<32;i++)
  142.     {
  143.         for (j=0;j<32;j++)
  144.         {
  145.             t = (smallTextureArray[i][j] + bigTextureArray[i][j][0]) / 2;
  146.             smallTextureArray[i][j] = (GLubyte) min(255,t);
  147.         }
  148.     }
  149. }
  150.  
  151. void MakeTexture()
  152. {
  153.     int i,j;
  154.     for (i=0;i<8;i++)
  155.     {
  156.         for (j=0;j<8;j++)
  157.         {
  158.             if (i==7 && j==7)
  159.             {
  160.                 AverageLastAndFirstTextures();
  161.             }
  162.             else
  163.             {
  164.                 MakeSmallTexture();
  165.             }
  166.             CopySmallTextureToBigTexture(i*32,j*32);
  167.         }
  168.     }
  169.  
  170.     glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  171.  
  172.     glGenTextures(1, &theTexture);     
  173.     glBindTexture(GL_TEXTURE_2D, theTexture);
  174.     
  175.     // Set the tiling mode (this is generally always GL_REPEAT).
  176.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  177.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  178.     
  179.     // Set the filtering.
  180.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  181.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  182.     
  183.     gluBuild2DMipmaps(GL_TEXTURE_2D, 2, 256, 256, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, bigTextureArray);
  184.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  185. }